package com.ztjava.ask.controller;

import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.kit.JsonKit;
import com.ztjava.ask.model.AskAnswer;
import com.ztjava.ask.model.AskCatalog;
import com.ztjava.ask.model.AskQuestion;
import com.ztjava.ask.service.AskService;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.PortalClient;
import com.ztjava.core.tool.ConfigTool;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;

@ControllerClass(path="/ask",client=PortalClient.class)
@UserController(portal=Constant.P_PORTAL)
public class AskPortalController extends BaseController{
	private static AskService askService = Duang.duang(AskService.class);
	/*************
	 *AskPortalController.java
	 *@AutoJavaCode
	 *2017-09-07 15:28:45
	**************/
	/*************
	 *修改Catalog
	**************/
	@UserMethod("AskCatalog")
	public void addEditCatalog(){
		Integer id = getParaToInt("id",0);
		AskCatalog askCatalog = new AskCatalog();
		if(id>0){
			askCatalog = AskCatalog.dao.findById(id);
			if(askCatalog.getInt("parent_id")>0){
					AskCatalog parentCatalog = AskCatalog.dao.findById(askCatalog.getInt("parent_id"));
					setAttr("parentCatalog", parentCatalog);
			}
		}else{
				Integer parent_id = getParaToInt("parent_id",0);
				askCatalog.set("parent_id", parent_id);
				if(parent_id>0){
					AskCatalog parentCatalog = AskCatalog.dao.findById(parent_id);
					setAttr("parentCatalog", parentCatalog);
				}
		}
		setAttr("askCatalog",askCatalog);
		render("addEditCatalog.html");
	}

	/*************
	 *保存Catalog
	**************/
	@UserMethod("AskCatalog")
	public void saveCatalog(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		AskCatalog askCatalog = getModel(AskCatalog.class);
		saveModel(askCatalog,authSession.getId());
		if(askCatalog.getInt("parent_id")>0){
			AskCatalog parent = AskCatalog.dao.findById(askCatalog.getInt("parent_id"));
			askCatalog.set("num", parent.getStr("num")+askCatalog.getInt("id")+",");
		}else {
			askCatalog.set("num", askCatalog.getInt("id")+",");
		}
		askCatalog.update();
		setAttr(Constant.RESULT, Constant.SUCCESS);
		setAttr(Constant.MESSAGE, "保存成功！");
		setAttr("object", askCatalog);
		ConfigTool.dbToBaseData("ASK_CATALOG",  AskCatalog.tablename, "sort asc,id asc");
		renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"object"});
	}

	/*************
	 *Catalog列表
	**************/
	@UserMethod("AskCatalog")
	public void catalogList(){
		
		List<AskCatalog> list = AskCatalog.dao.find("select id,parent_id as pId,name from "+AskCatalog.tablename+" where 1=1 "
				+" order by sort asc,id asc");
		setAttr("listJSON", JsonKit.toJson(list));
		render("catalogList.html");
	}

	/*************
	 *删除Catalog
	**************/
	@UserMethod("AskCatalog")
	public void deleteCatalog(){
		Integer id = getParaToInt("id",0);
		AskCatalog child = AskCatalog.dao.findFirst("select id from "+AskCatalog.tablename+" where parent_id=?",id);
		if(child!=null){
			renderError("存在子分类不允许删除！");
			return;
		}
		AskCatalog.dao.deleteById(id);
		setAttr(Constant.RESULT, Constant.SUCCESS);
		setAttr(Constant.MESSAGE, "删除成功！");
		setAttr("id", id);
		ConfigTool.dbToBaseData("ASK_CATALOG",  AskCatalog.tablename, "sort asc,id asc");
		renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"id"});
	}
	
	/*************
	 *修改Question
	**************/
	@UserMethod("AskQuestion")
	public void addEditQuestion(){
		Integer id = getParaToInt("id",0);
		AskQuestion askQuestion = new AskQuestion();
		if(id>0){
			askQuestion = AskQuestion.dao.findById(id);
		}else{
				askQuestion.set("catalog_id",getParaToInt("catalog_id",0));
		}
		setAttr("askQuestion",askQuestion);
		render("addEditQuestion.html");
	}
	
	/*************
	 *保存Question
	**************/
	@UserMethod("AskQuestion")
	public void saveQuestion(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		AskQuestion askQuestion = getModel(AskQuestion.class);
		saveModel(askQuestion,authSession.getId());
			renderSuccess();
	}
	
	/*************
	 *Question列表
	**************/
	@UserMethod("AskQuestion")
	public void questionList(){
		Integer catalog_id = getParaToInt("catalog_id");
		setAttr("catalog_id",catalog_id);
		if(!getPara("is_ajax","F").equals("T")){
			List<AskCatalog> list = AskCatalog.dao.find("select id,parent_id as pId,name,num from "+AskCatalog.tablename+" order by sort asc,id asc");
			setAttr("listJSON", JsonKit.toJson(list));
			render("questionList.html");
			return;
		}
		renderPageJson(askService.getQuestionPage(client, getParaMap()));
	}
	
	/*************
	 *批量删除Question
	**************/
	@UserMethod("AskQuestion")
	public void batchDeleteQuestion(){
		String[] btSelectItem = getParaValues("btSelectItem");
		askService.batchDelete(AskQuestion.tablename, btSelectItem);
		renderSuccess();
	}

	/*************
	 *修改Answer
	**************/
	@UserMethod("AskQuestion")
	public void addEditAnswer(){
		Integer id = getParaToInt("id",0);
		AskAnswer askAnswer = new AskAnswer();
		if(id>0){
			askAnswer = AskAnswer.dao.findById(id);
		}else{
				askAnswer.set("question_id",getParaToInt("question_id",0));
		}
		setAttr("askAnswer",askAnswer);
		render("addEditAnswer.html");
	}
	
	/*************
	 *保存Answer
	**************/
	@UserMethod("AskQuestion")
	public void saveAnswer(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		AskAnswer askAnswer = getModel(AskAnswer.class);
		saveModel(askAnswer,authSession.getId());
		
			renderSuccess();
	}
	
	/*************
	 *Answer列表
	**************/
	@UserMethod("AskQuestion")
	public void answerList(){
		Integer question_id = getParaToInt("question_id");
		setAttr("question_id",question_id);
		if(!getPara("is_ajax","F").equals("T")){
			render("answerList.html");
			return;
		}
		renderPageJson(askService.answerPage(client, getParaMap()));
	}
	/***
	 * 采纳答案
	 */
	@UserMethod("AskQuestion")
	public void goodAnswer(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.goodAnswerAdmin(getParaToInt("answer_id"), authSession.getId()));
	}
	/*************
	 *批量删除Answer
	**************/
	@UserMethod("AskQuestion")
	public void batchDeleteAnswer(){
		String[] btSelectItem = getParaValues("btSelectItem");
		askService.batchDelete(AskAnswer.tablename, btSelectItem);
		renderSuccess();
	}

}
